package com.ssy.lingxi.order.serviceimpl.base;

import com.ssy.lingxi.common.constant.contract.ContractSourceTypeEnum;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.order.entity.OrderContractDO;
import com.ssy.lingxi.order.entity.OrderDO;
import com.ssy.lingxi.order.model.constant.OrderServiceContants;
import com.ssy.lingxi.order.model.vo.basic.request.OrderContractVO;
import com.ssy.lingxi.order.model.vo.basic.request.SrmOrderContractVO;
import com.ssy.lingxi.order.model.vo.basic.response.OrderContractDetailVO;
import com.ssy.lingxi.order.repository.OrderContractRepository;
import com.ssy.lingxi.order.service.base.IBaseOrderContractService;
import com.ssy.lingxi.order.utils.NumberUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 订单合同相关接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-07-19
 */
@Service
public class BaseOrderContractServiceImpl implements IBaseOrderContractService {
    @Resource
    private OrderContractRepository orderContractRepository;

    /**
     * 校验并生成订单合同，调用方需要保存OrderDO
     *
     * @param order      订单
     * @param contractVO 订单合同接口参数
     * @param isCreate 是否新增， true-新增，false-修改
     * @return 校验结果
     */
    @Override
    public Wrapper<Void> checkOrderContract(OrderDO order, OrderContractVO contractVO, boolean isCreate) {
        if(!isCreate) {
            orderContractRepository.deleteByOrder(order);
        }

        //订单可以不需要合同
        if(contractVO == null) {
            order.setContract(null);
            return Wrapper.success();
        }

        OrderContractDO contract = new OrderContractDO();
        contract.setOrder(order);
        contract.setContractId(0L);
        contract.setContractNo("");
        contract.setDigest("");
        contract.setEffectDate(null);
        contract.setExpireDate(null);
        contract.setPartB("");
        //采购订单没有合同类型，设置为Null
        contract.setContractType(null);
        contract.setLeftAmount(null);
        contract.setReceiptNo("");
        contract.setFileName(contractVO.getFileName());
        contract.setUrl(contractVO.getUrl());

        order.setContract(contract);
        return Wrapper.success();
    }

    /**
     * 校验并生成Srm订单合同，调用方需要保存OrderDO
     *
     * @param order      订单
     * @param contractVO 订单合同接口参数
     * @param isCreate 是否新增， true-新增，false-修改
     * @return 校验结果
     */
    @Override
    public Wrapper<Void> checkSrmOrderContract(OrderDO order, SrmOrderContractVO contractVO, boolean isCreate) {
        if(!isCreate) {
            orderContractRepository.deleteByOrder(order);
        }

        //Srm订单必须有合同
        OrderContractDO contract = new OrderContractDO();
        contract.setOrder(order);
        contract.setContractId(contractVO.getContractId());
        contract.setContractNo(contractVO.getContractNo());
        contract.setDigest(contractVO.getDigest());
        contract.setEffectDate(LocalDateTime.parse(contractVO.getEffectDate().concat(" 00:00:00"), OrderServiceContants.DEFAULT_TIME_FORMATTER));
        contract.setExpireDate(LocalDateTime.parse(contractVO.getExpireDate().concat(" 23:59:59"), OrderServiceContants.DEFAULT_TIME_FORMATTER));
        contract.setPartB(contractVO.getPartB());
        contract.setContractType(contractVO.getContractType());
        contract.setLeftAmount(contractVO.getLeftAmount());
        contract.setReceiptNo(StringUtils.hasLength(contractVO.getReceiptNo()) ? contractVO.getReceiptNo() : "");
        contract.setFileName("");
        contract.setUrl("");

        order.setContract(contract);
        return Wrapper.success();
    }

    /**
     * （转单） - 生成合同
     *
     * @param order      订单
     * @param contractId 交易流程中配置的合同Id
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> transferOrderContract(OrderDO order, Long contractId) {
        //根据合同Id是否大于0来判断
        if(NumberUtil.isNullOrZero(contractId)) {
            order.setContract(null);
            return Wrapper.success();
        }

        OrderContractDO contract = new OrderContractDO();
        contract.setOrder(order);
        contract.setContractId(contractId);
        contract.setContractNo("");
        contract.setDigest("");
        contract.setEffectDate(null);
        contract.setExpireDate(null);
        contract.setPartB("");
        contract.setContractType(null);
        contract.setLeftAmount(null);
        contract.setReceiptNo("");
        contract.setFileName("");
        contract.setUrl("");

        order.setContract(contract);
        return Wrapper.success();
    }

    /**
     * 查询订单合同信息
     *
     * @param order 订单
     * @return 合同信息
     */
    @Override
    public OrderContractDetailVO getOrderContract(OrderDO order) {
        OrderContractDO contract = orderContractRepository.findFirstByOrder(order);
        if(contract == null) {
            return null;
        }

        OrderContractDetailVO detailVO = new OrderContractDetailVO();
        detailVO.setContractId(contract.getContractId());
        detailVO.setContractNo(contract.getContractNo());
        detailVO.setDigest(contract.getDigest());
        detailVO.setEffectDate(contract.getEffectDate() == null ? "" : contract.getEffectDate().format(OrderServiceContants.DEFAULT_DATE_FORMATTER));
        detailVO.setExpireDate(contract.getExpireDate() == null ? "" : contract.getExpireDate().format(OrderServiceContants.DEFAULT_DATE_FORMATTER));
        detailVO.setPartB(contract.getPartB());
        detailVO.setContractType(contract.getContractType() == null ? -1 : contract.getContractType());
        detailVO.setContractTypeName(contract.getContractType() == null ? "" : ContractSourceTypeEnum.getNameByCode(contract.getContractType()));
        detailVO.setLeftAmount(contract.getLeftAmount() == null ? "" : NumberUtil.formatAmount(contract.getLeftAmount()));
        contract.setReceiptNo(contract.getReceiptNo());
        detailVO.setFileName(contract.getFileName());
        detailVO.setUrl(contract.getUrl());
        return detailVO;
    }
}
